#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# runtime
############################################################################
#    Copyright (C) 2007 by Håvard Gulldahl                                 #
#    havard@gulldahl.no                                                    #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import sys, types, logging, os

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    stream=sys.stdout)

logging.debug('importerer BeautifulSoup')
import BeautifulSoup
logging.debug('importerer storm')
import storm.locals as storm
logging.debug('importerer jeje')
import jeje, jeje.fingerprinter, jeje.collector

DEBUG=False
_store = None
try:
    db_pass = open('dbpass.txt').readline().strip()
except:
    db_pass = ''
conn_string = 'mysql://ejeje:%s@localhost/ejeje' % db_pass

def prepare_database():

    logging.debug('Kobler til storm:')
    database = storm.create_database(conn_string)
    store = storm.Store(database)
    #Note that Store objects are not threadsafe. You should create one Store per thread in your application, passing them the same backend Database object
    jeje._store = store # FIXME: make a proper connection manager
    if 0:
        logging.debug('Lager tabeller')
        store.execute(jeje.ejeje_saksbehandler._sql)#createTable(ifNotExists=True)
        store.execute(jeje.ejeje_organisasjon._sql)
        store.execute(jeje.ejeje_adressat._sql)
        store.execute(jeje.ejeje_postjournal._sql)
        store.execute(jeje.ejeje_journalpost._sql)
        store.execute(jeje.ejeje_postjournal_logg._sql)
        store.commit()
    return store

def test_saksbehandler():
    s = jeje.ejeje_saksbehandler('jorunn fjellgard')
    s.sett_organisasjon('hjemmefra')
    return jeje._store.add(s)

def test_lookup():
    import jeje.lookup

    iface = jeje.lookup.ejeje_lookup(jeje._store)
    r = iface.search('avvik')


if __name__ == '__main__':
    s = prepare_database()

    c = jeje.collector.collector(s)
    c.collect()
    s.commit()

